﻿using System;
using System.Linq;

namespace IndentationSplitter
{
    internal class IndentedLine
    {
        private readonly string _text;

        public IndentedLine(string text)
        {
            _text = text;
            Indentation = GetIndentation(_text);
        }

        public string Text { get { return _text.TrimStart('\t'); } }

        public int Indentation { get; private set; }

        private static int GetIndentation(string line)
        {
            for (int i = 0; i < line.Length; ++i)
                if (line[i] != '\t')
                    return i;

            throw new FormatException("Line consists of only tabs!");
        }

        public static IndentedLine[] GetLinesFromText(string text)
        {
            text = new IndentationFormat(text).TabbedText;
            return text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
                       .Select(l => new IndentedLine(l))
                       .ToArray();
        }
    }
}